package com.neuedu.dao;



import com.neuedu.entity.Dept;
import com.neuedu.utils.DBUtils;

import java.util.List;
import java.util.Map;

public class DeptDao {

    /**
     * 根据条件查询集合
     * @param where
     * @param params
     * @return
     */
    //where  =      where sss = ?
    public List<Dept> selectList(String where , Object ... params){
        String sql =" select * from dept ";
        if(where != null && !"".equals(where)){
            sql += where;
        }
        System.out.println("sql = " + sql);
        List<Dept> depts = DBUtils.selectList(Dept.class, sql, params);

        return depts;
    }

    /**
     * 分页查询
     * @param where
     * @param pageNo
     * @param size
     * @param params
     * @return
     */
    public List<Dept> selectPage(String where , int pageNo,int size ,Object ... params){
        String sql =" select * from dept ";
        if(where != null && !"".equals(where)){
            sql += where;
        }

        sql += " limit "+(pageNo-1)*size+"   ,"+size;

        System.out.println("sql = " + sql);
        List<Dept> depts = DBUtils.selectList(Dept.class, sql, params);

        return depts;
    }


    /**
     * 根据条件查询一个
     * @param where
     * @param params
     * @return
     */
    public Dept selectOne(String where ,Object ... params){
        String sql =" select * from dept ";
        if(where != null && !"".equals(where)){
            sql += where;
        }
        return DBUtils.selectOne(Dept.class, sql, params);
    }


    /**
     * 查询总条数
     * @param where
     * @param params
     * @return
     */
    public long selectCount(String where ,Object ... params){
        String sql =" select * from dept ";
        if(where != null && !"".equals(where)){
            sql += where;
        }

        sql = "select count(1) count from ("+ sql  +") tab   ";
        Map map = DBUtils.selectMap( sql , params);
        long count = (long) map.get("count");
        return count;
    }

    /**
     * 根据数据进行插入
     * @param dept
     * @return
     */
    public int insert(Dept dept){
        String sql = "insert into dept(deptno,dname,loc ) values(?,?,?)";
        return DBUtils.update( sql, dept.getDeptno(),dept.getDname(),dept.getLoc());
    }


    /**
     *  批量插入
     * @param deptList
     * @return
     */
    public int insertBatch(List<Dept> deptList){
        String sql = "insert into dept(deptno,dname,loc ) values(?,?,?)";
        int count = 0;
        for (Dept dept : deptList) {
            count += DBUtils.update( sql,
                    dept.getDeptno(),
                    dept.getDname(),
                    dept.getLoc()
            );

        }
        return count;
    }


    /**
     * 根据主键更新一条
     * @param dept
     * @return
     */
    public int updateById(Dept dept){
        String sql = " update dept set dname = ? , loc = ? where deptno = ?";
        return DBUtils.update( sql,
                    dept.getDname(),
                    dept.getLoc(), dept.getDeptno());

    }


    /**
     * 根据主键更新一条
     * @param id
     * @return
     */
    public int deleteById(Integer id){
        String sql = " delete from dept  where deptno = ?";
        return DBUtils.update( sql, id);

    }


    /**
     * 根据条件删除
     * @param where
     * @param params
     * @return
     */
    public int delete(String where ,Object... params){
        String sql = " delete from dept ";
        if(where == null || "".equals(where)){
            throw new RuntimeException("必须有条件删除");
        }
        sql += where;
        return DBUtils.update( sql, params);

    }

}
